package com.mes.web.controller.jy;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.entity.hy.Product;
import com.mes.entity.hy.Work;
import com.mes.entity.jy.Gotempku;
import com.mes.entity.jy.Gpk;
import com.mes.entity.jy.Queryjy;
import com.mes.entity.jy.Tempproduct;
import com.mes.service.jy.GotempkuServise;
import com.mes.service.jy.ProductPlanServise;
import com.mes.utils.ResponseUtil;

/**
 * @ClassName: GotempkuController 
 * @Description:产品入库
 * @Author jy
 * @Date 2018年1月26日 下午3:30:52 
 * @version 1.0
 */
@Controller
@RequestMapping("/gotempku")
public class GotempkuController {

	@Resource
	private GotempkuServise gotempkuServise;
	@Resource
	private ProductPlanServise productPlanServise;
	
	/**
	 * 添加产品入库(android)
	 * @param gpk 入库单
	 * @param request 请求
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/createGotempku")
	public void createGotempku(String gpk,HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		//影响行数
		int affact = 0;
		
		//解析条码集合
		List<String> codes = new ArrayList<>();
		JSONObject jsonObject=JSONObject.fromObject(gpk);
		JSONArray jsonArray=jsonObject.getJSONArray("codes");
		for (int i = 0; i < jsonArray.size(); i++) {
			String code=(String)jsonArray.get(i);
			codes.add(code);
		}
		
		//将数据解析成实体类
		Gpk gpk1=(Gpk)JSONObject.toBean(jsonObject, Gpk.class);
		//根据产品id查询产品信息
		Product product = productPlanServise.selsectProductByid(Long.valueOf(gpk1.getPid()));
		
		//选中记录的待入库数量
		Integer dnum = Integer.valueOf(gpk1.getDnum());
		
		//添加入库单
		int id = gotempkuServise.createGpk(gpk1);
		gpk1.setId(Long.valueOf(id));
		List<String> codes2 = gpk1.getCodes();
		
		//容器设定值
		Integer rnum = Integer.valueOf(gpk1.getRnum());
		//条码个数
		int size = codes2.size();
		//容器设定值*条码个数 = 入库数量
		int ru = rnum * size;
		//判断，入库数量是否大于待入库数量
		if( ru > dnum ) {
			//入库数量大于待入库数量，错误提示：入库数量过多！
			affact = -1;
		}else if( ru == dnum) {
			//做入库操作，并将入库状态改为1
			for (String code : codes2) {
				/*入库前先判断此条码是否已经存在  存在：提示条码已存在， 不存在：放行（暂未实现）
				Gotempku gotempku1 = gotempkuServise.selectCodeExist(code);
				入库详情*/
				Gotempku gotempku = new Gotempku();
				gotempku.setDatatime(gpk1.getGodate());
				gotempku.setPihao(gpk1.getPici());
				gotempku.setRemark(gpk1.getPid());
				gotempku.setRemark2(gpk1.getId()+"");
				gotempku.setCode(code);
				gotempku.setNum(rnum+"");
				//添加入库详情
				affact = gotempkuServise.createGotempku(gotempku);
				
				//库存
				Tempproduct tempproduct = new Tempproduct();
				tempproduct.setDatatime(gpk1.getGodate());
				tempproduct.setRemark(gpk1.getPici());
				tempproduct.setPid(gpk1.getPid());
				tempproduct.setRemark1(code);
				tempproduct.setRemark2(rnum+"");
				tempproduct.setRemark7("0");
				//添加库存
				affact = gotempkuServise.createTempProduct(tempproduct);
				
				//根据作业信息id查询作业信息
				Work work = gotempkuServise.selectAllwByid(gpk1.getWorkid());
				if(product.getNum().equals(work.getNmum()) && gpk1.getDnum().equals(work.getRemartk5())) {
					//修改作业信息中的入库状态remartk7
					gotempkuServise.editAllwState(gpk1.getWorkid());
				}else if(product.getNum().equals(work.getNmumr()) && gpk1.getDnum().equals(work.getRemartk6())) {
					//修改作业信息中的入库状态remartk8
					gotempkuServise.editAllwState2(gpk1.getWorkid());
				}
				gotempku = null;
				tempproduct = null;
			}
		}else {
			//做入库操作，待入库数量 - 入库数量 = 剩余待入库数量，修改剩余待入库数量的值
			for (String code : codes2) {
				//入库前先判断此条码是否已经存在  存在：提示条码已存在， 不存在：放行（暂未实现）
				/*Gotempku gotempku1 = gotempkuServise.selectCodeExist(code);*/
				//入库详情
				Gotempku gotempku = new Gotempku();
				gotempku.setDatatime(gpk1.getGodate());
				gotempku.setPihao(gpk1.getPici());
				gotempku.setRemark(gpk1.getPid());
				gotempku.setRemark2(gpk1.getId()+"");
				gotempku.setCode(code);
				gotempku.setNum(rnum+"");
				//添加入库详情
				affact = gotempkuServise.createGotempku(gotempku);
				
				//库存
				Tempproduct tempproduct = new Tempproduct();
				tempproduct.setDatatime(gpk1.getGodate());
				tempproduct.setRemark(gpk1.getPici());
				tempproduct.setPid(gpk1.getPid());
				tempproduct.setRemark1(code);
				tempproduct.setRemark2(rnum+"");
				tempproduct.setRemark7("0");
				//添加库存
				affact = gotempkuServise.createTempProduct(tempproduct);
				
				dnum = dnum - ru;
				//根据作业信息id查询作业信息
				Work work = gotempkuServise.selectAllwByid(gpk1.getWorkid());
				if(product.getNum().equals(work.getNmum()) && gpk1.getDnum().equals(work.getRemartk5())) {
					//修改作业信息中的remartk5
					work.setRemartk5(dnum+"");
					gotempkuServise.editAllwR5(work);
				}else if(product.getNum().equals(work.getNmumr()) && gpk1.getDnum().equals(work.getRemartk6())) {
					//修改作业信息中的remartk6
					work.setRemartk6(dnum+"");
					gotempkuServise.editAllwR6(work);
				}
				gotempku = null;
				tempproduct = null;
			}
		}
		
		net.sf.json.JSONObject jsonObject1 = new net.sf.json.JSONObject();
		if (affact ==1 ) {
			jsonObject1.put("code", "200");
    		jsonObject1.put("message", "入库成功");
    		jsonObject1.put("results", null);
		}else if(affact == -1) {
			jsonObject1.put("code", "500");
    		jsonObject1.put("message", "入库数量过多");
    		jsonObject1.put("results", null);
		}else {
			jsonObject1.put("code", "500");
    		jsonObject1.put("message", "入库失败");
    		jsonObject1.put("results", null);
		}
		ResponseUtil.write(response, jsonObject1);
	}
	
	/**
	 * 带条件的分页查询入库单详情
	 * @param queryjy 条件查询实体
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectGotempkuPagein")
	public void selectGotempkuPagein(Queryjy queryjy,HttpServletResponse response) throws Exception {
		//统计总数
		int total = gotempkuServise.getTotal(queryjy);
		//入库详情实体集合
		List<Gotempku> list = gotempkuServise.selectGotempkuPagein(queryjy);
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 带条件的分页查询入库单
	 * @param queryjy 条件查询实体
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectGpkPagein")
	public void selectGpkPagein(Queryjy queryjy,HttpServletResponse response) throws Exception {
		//统计总数
		int total = gotempkuServise.getTotalGpk(queryjy);
		//入库实体集合
		List<Gotempku> list = gotempkuServise.selectGpkPagein(queryjy);
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
}
